package com.acme.bankapp.domain.bank;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({"StringBufferMayBeStringBuilder", "UnusedDeclaration"})
public class Client {
    List<Account> accounts;

    Gender gender;

    String name;

    public Client() {
        accounts = new ArrayList<Account>();
    }

    public Gender getGender() {
        return gender;
    }


    public String getName() {
        return name;
    }

    public Client(String name, Gender gender) {
        this();
        this.gender = gender;
        this.name = name;
    }

    public List<Account> getAccounts() {
        return accounts;
    }

    public void addAccount(Account acc) {
        this.accounts.add(acc);
    }

    private String getClientSalutation() {
        if (gender != null && name != null) {
            return gender + " " + name;
        } else {
            return "";
        }
    }

    public String toString() {
        String salutation = getClientSalutation();
        StringBuffer result = new StringBuffer("");
        if (salutation.equals("")) {
            result.append(super.toString());
        } else {
            result.append(salutation);
        }
        result .append(", accounts ").append(accounts);
        return result.toString();
    }

    @SuppressWarnings("RedundantIfStatement")
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Client client = (Client) o;
        //XXX [MY] Comparison of business keys enough
        if (accounts != null ? !accounts.equals(client.accounts) : client.accounts != null) return false;
        if (gender != client.gender) return false;
        if (name != null ? !name.equals(client.name) : client.name != null) return false;

        return true;
    }

    public int hashCode() {
        int result = accounts != null ? accounts.hashCode() : 0;
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}
